package com.nuit.cxl.ch2.controller;

import com.nuit.cxl.ch2.config.Myconfig;
import com.nuit.cxl.ch2.mapper.UserMapper;
import com.nuit.cxl.ch2.service.HelloWorldService;
import com.nuit.cxl.ch2.service.HelloWorldServiceImpl;
import com.nuit.cxl.ch2.service.HelloYouServiceImpl;
import com.nuit.cxl.ch2.vo.Student;
import com.nuit.cxl.ch2.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

// alt + enter
//接受 请求
@RestController
@RequestMapping("/new")
public class HelloWorld {

    @Autowired
    @Qualifier("HelloWorldServiceImpl")
    public HelloWorldService helloWorldService;
    @Autowired
    public Myconfig myconfig;
    @Autowired
    public UserMapper userMapper;

    Student student = new Student();

    Logger logger = Logger.getLogger(HelloWorld.class.getName());

    @GetMapping("/hello")
    @ResponseBody
    public String helloWorld() {
        // Integer  int  float  boolean
        // sout
        //（）；  中文符号不能用
//        System.out.println("run this");
        logger.warning("logger");
        return "hello";
    }

//    @RequestMapping("/student")
//    public Student student() {
//        Student student = new Student();
//        student.setAge(20);
//        student.setName("Jack");
//        return student;
//    }

    @RequestMapping("/hellowithparam")
    public String helloWorldwithParm(@RequestParam() String name,
                                     @RequestParam() String age) {
        System.out.println(name + "  " + age);
        return name + age;
    }

    @RequestMapping("/student-body")
    public Student studentBody(@RequestBody Student student) {
        System.out.println(student);
        return student;
    }

    @RequestMapping("/student/{id}")
    public Student studentId(@PathVariable String id) {
//        System.out.println(student);

        Student student1 = userMapper.selectById(id);
        return student1;
    }

    @RequestMapping("/student")
    public List<StudentVO> getStudentByAge(@RequestParam Integer age){
//        System.out.println(student);
        List<Student> studentList = userMapper.selectByAge(age);
        ArrayList<StudentVO> studentVOArrayList = new ArrayList<>();
        for (Student student : studentList) {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);
            studentVOArrayList.add(studentVO);
        }
    return studentVOArrayList;
    }

    // pathVariable  后面的名字要对应 url上面的这个ids  一模一样
    @RequestMapping("/{ids}/student")
    public String studentIdFront(@PathVariable String ids) {
//        System.out.println(student);
        return ids;
    }


    @RequestMapping("/getStudent")
    public Student getStudent() {
        logger.warning("getStudent");
        Student s = helloWorldService.getStudent();
        return s;
    }

    @RequestMapping("/getConfig")
    public String getConfig() {
        String name = myconfig.name;
        return name;
    }
}
